<?php
class Common2_model extends CI_Model {
    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    //用户信息
    function userInfo(){
    	$openid = $this->session->userdata('openid')?$this->session->userdata('openid'):"error";//$openid
    	$query = $this->db->get_where("users", array("Identifier"=>strval($openid)), 1);
    	return $query->row_array();
    }
    //用户收藏
    function usercollected(){
        $this->db->select("u.id,c.title,c.goodsUrl,c.id contentid,c.picture,c.currentPrice");
        $this->db->where("u.user",$this->session->userdata('openid'));
        $this->db->where("c.id IS NOT NULL");
        $this->db->from("usercollected u");
        $this->db->join('content c', ' c.id = u.contentid AND u.`from`=1 AND c.status=1 ','left');
        $query    = $this->db->get();
        $num_rows = $query->num_rows();//记录数
        $data     = $query->result_array(); //结果
        $rs['ret'] = 0;
        if ($num_rows>0) {
        	$rs['ret']  = $num_rows;
        	$rs['data'] = $data;
        }
        return $rs;
        //echo $this->db->last_query();
    }
    //删除收藏
    function usercollecteddel($id){
    	$this->db->delete("usercollected",array("id"=>$id));
    	$rs['ret'] = $this->db->affected_rows();
    	//$rs['ret'] = $this->db->last_query();
    	return $rs;
    }
    //签到
    function qiaodao(){
    	$openid = $this->session->userdata('openid')?$this->session->userdata('openid'):"error";//$openid
    	if ($openid=="error" || $openid===0) {
    		return array("ret"=>-200,"msg"=>"请重新登录");
    	}
    	$user = $this->userInfo();
        if($user['LatestCheckedIn']!=date("Y-m-d")){//还没签到
            $addGoldCoin = ($user['ContinuousLoginTime']>=5)?200:100;//添加的金币
            $this->db->set("LatestCheckedIn","now()",FALSE);
            $this->db->set("GoldCoinNumber","GoldCoinNumber+".$addGoldCoin,FALSE);
            $this->db->where("Identifier",strval($openid));
            $this->db->update("users");
			//更改成功
            $rs['ret']       = $this->db->affected_rows();
            $rs['addGold']   = $addGoldCoin;
            $rs['totalGold'] = $user['GoldCoinNumber']+$addGoldCoin;
            $rs['ContinuousLoginTime'] = $user['ContinuousLoginTime'];
            //金币操作记录
            $data = array(
            	'openid'   => strval($openid),
            	'gold'     => $addGoldCoin,
            	'gold_old' => $user['GoldCoinNumber'],
            	'gold_new' => $rs['totalGold'],
            	'action'   => '签到',
            	'time'     => date('Y-m-d H:i:s')
            );
            $this->db->insert('users_gold_add',$data);
            return $rs;
        }else{
            return array('ret'=>0,'addGold'=>0,'totalGold'=>$user['GoldCoinNumber'],'ContinuousLoginTime'=>$user['ContinuousLoginTime']);
        }
    }
    //关注
    function guanzhu($fanQQ){
    	$openid = $this->session->userdata('openid')?$this->session->userdata('openid'):"error";//$openid
    	if ($openid=="error" || $openid===0) {
    		return array("ret"=>-200,"msg"=>"请重新登录");
    	}
    	$user = $this->userInfo();
    	if($fanQQ=='80249498'){ //返还网
    	    $no_first=$user['IsFirstFollowUs'];
    	    $changeCol = "IsFirstFollowUs";
    	}
    	if($fanQQ=='861121')  { //方毅
    	    $no_first=$user['Ex_FirstFollowFangyi'];
    	    $changeCol = "Ex_FirstFollowFangyi";
    	}
    	if($fanQQ=='783350315')  { //upin
    	    $no_first=$user['Ex_FirstFollowUpin'];
    	    $changeCol = "Ex_FirstFollowUpin";
    	}
    	if($fanQQ=='2202411011')  { //内涵图
    	    $no_first=$user['Ex_FirstFollowNeihan'];
    	    $changeCol = "Ex_FirstFollowNeihan";
    	}
    	if($no_first==1){
    	    return array('ret'=>0,'addGold'=>0,'totalGold'=>$user['GoldCoinNumber']);
    	}else{
    	    $totalGold = $user['GoldCoinNumber']+500;
    	    //更改金币和状态
    	    $this->db->set($changeCol,1);
    	    $this->db->set("GoldCoinNumber",$totalGold);
    	    $this->db->where("Identifier",strval($openid));
    	    $this->db->update("users");
    	    //金币操作记录
    	    $data = array(
    	    		'openid' => strval($openid),
    	    		'gold'   => 500,
    	    		'gold_old' => $user['GoldCoinNumber'],
    	    		'gold_new' => $totalGold,
    	    		'action' => "关注",
    	    		'time'   => date('Y-m-d H:i:s')
    	    );
    	    $this->db->insert('users_gold_add',$data);
    	    return array('ret'=>1,'addGold'=>500,'totalGold'=>$totalGold);
    	}
    }
    //面板
    function onpanel(){
    	$openid = $this->session->userdata('openid')?$this->session->userdata('openid'):"error";//$openid
    	if ($openid=="error" || $openid===0) {
    		return array("ret"=>-200,"msg"=>"请重新登录");
    	}
    	$user = $this->userInfo();
        $totalGold = $user['GoldCoinNumber'];//总金币
        $addGold = 0;//添加的金币
        if ($user['isGetAppOnPanelGold']==0) {
            $addGold = 500;
        	$totalGold = $totalGold+$addGold;
        	//更改金币和状态
        	$this->db->set("isGetAppOnPanelGold",1);
        	$this->db->set("GoldCoinNumber",$totalGold);
        	$this->db->where("Identifier",strval($openid));
        	$this->db->update("users");
        	//金币操作记录
        	$data = array(
        			'openid' => strval($openid),
        			'gold'   => $addGold,
        			'gold_old' => $user['GoldCoinNumber'],
        			'gold_new' => $totalGold,
        			'action' => "面板",
        			'time'   => date('Y-m-d H:i:s')
        	);
        	$this->db->insert('users_gold_add',$data);
        }
        return array("ret"=>1,"addGold"=>$addGold,"totalGold"=>$totalGold);
    }
    //用户分享
    function userShared(){
    	$openid = $this->session->userdata('openid')?$this->session->userdata('openid'):"error";//$openid
    	if ($openid=="error" || $openid===0) {
    		return array("ret"=>-200,"msg"=>"请重新登录");
    	}
    	$user = $this->userInfo();
        $totalGold = $user['GoldCoinNumber'];//总金币
        $addGold = 0;//添加的金币
        if($user['ShareDate']!=date("Y-m-d")){
            $addGold = 100;
            $totalGold = $totalGold+$addGold;
            //更改金币和状态
            $this->db->set("ShareDate",date("Y-m-d"));
            $this->db->set("GoldCoinNumber",$totalGold);
            $this->db->where("Identifier",strval($openid));
            $this->db->update("users");
            //金币操作记录
            $data = array(
            		'openid' => strval($openid),
            		'gold'   => $addGold,
            		'gold_old' => $user['GoldCoinNumber'],
            		'gold_new' => $totalGold,
            		'action' => '分享',
            		'time'   => date('Y-m-d H:i:s')
            );
            $this->db->insert('users_gold_add',$data);
        }
        return array("ret"=>1,"addGold"=>$addGold,"totalGold"=>$totalGold);
    }
    //邀请好友
    function yaoqingORzhaohuan($action){
    	$openid = $this->session->userdata('openid')?$this->session->userdata('openid'):"error";//$openid
    	if ($openid=="error" || $openid===0) {
    		return array("ret"=>-200,"msg"=>"请重新登录");
    	}
    	$user = $this->userInfo();
        $actionCol = ($action=="yaoqing")?"yaoqingTime":"zhaohuanTime";
        $actiontime = $user[$actionCol];
        
        $addGold = 0;//添加的金币
        $totalGold = $user['GoldCoinNumber'];//总金币
        if ($actiontime<3) {
        	$addGold = 100;//添加的金币
            $totalGold = $totalGold+$addGold;//总金币
            //更改金币和状态
            $this->db->set($actionCol,$actionCol."+1",FALSE);
            $this->db->set("GoldCoinNumber",$totalGold);
            $this->db->where("Identifier",strval($openid));
            $this->db->update("users");
            //金币操作记录
            $data = array(
            		'openid'   => strval($openid),
            		'gold'     => $addGold,
            		'gold_old' => $user['GoldCoinNumber'],
            		'gold_new' => $totalGold,
            		'action'   => '邀请、召唤',
            		'time'     => date('Y-m-d H:i:s')
            );
            $this->db->insert('users_gold_add',$data);
        }
        return array("ret"=>1,"addGold"=>$addGold,"totalGold"=>$totalGold);
    }
    //兑换商品
    function AjaxTaskExchangeGoods($date=null){
    	$date = $date?$date:date("Y-m-d");
    	$this->db->select("url,name,pic");
    	//$this->db->select("if( HOUR(NOW())>=10,if(`exchangeNum`>0,1,0),-1) `isUse`",false);
    	$this->db->where(array('pDate'=>$date,'status'=>1 ,'isDel'=>0));
    	$query = $this->db->get('zz_extend_exchangegoods', 3);
    	$goods = $query->result_array();
    	return array("ret"=>1,"goods"=>$goods);
    }
    //统计
    function AjaxStatistics($name){
    	$this->db->select("id");
    	$this->db->where("name",$name);
    	$this->db->where("date",date("Y-m-d"));
    	$query = $this->db->get("keyvaluebydate");
    	$num = $query->num_rows();//是否今天有统计数据
    	$id  = $query->row_array();//id
    	//操作
    	if ($num>0) {//
    		$this->db->set("num","num+1",false);
    		$this->db->where("name",$name);
    		$this->db->where("date",date("Y-m-d"));
    		$this->db->update("keyvaluebydate");
    	}else{
    		$this->db->set("num","1",false);
    		$this->db->set("name",$name);
    		$this->db->set("date",date("Y-m-d"));
    		$this->db->insert("keyvaluebydate");
    	}
    	return $this->db->affected_rows();
    }
    /* ---------------------------------------------转盘------------------------------------------ */
    //1.获奖用户
    function AjaxZhuanPanAwardUser(){
    	$size = 20;
    	//今天的数据
    	$today = date("Y-m-d");
    	$query = $this->db->get_where("zz_extend_useraward",array("turndate"=>$today));
    	$todaydata = $query->result_array();
    	$todaydataNum = $query->num_rows();
    	if ($todaydataNum==$size){//返回今天数据
    		return $todaydata;
    	}else{//今日没数据
    		$yesterday = date("Y-m-d",strtotime("-1 day"));
    		//昨天的数据最大id
    		$this->db->select("MAX(`id`) yesterday_id",false);
    		$this->db->where("turndate",$yesterday);
    		$query = $this->db->get("zz_extend_useraward");
    		$yesterday_max_id = $query->row_array();
    		$yesterday_max_id = $yesterday_max_id['yesterday_id'];//id
    		$yesterday_max_id_num = $query->num_rows();//id数量
    		if ($yesterday_max_id_num>0) {//有最大id
    			//今天的size条记录
    			$todaydataArray = $this->getAwardUser($size,$yesterday_max_id);
    			//如果等于size
    			if ($todaydataArray['num']==$size) {
    				//更改表
    				$this->updateAwardUserDate($todaydataArray['ids']);
    				//返回结果
    				return $todaydataArray['data'];
    			}else {
    				$todaydataArray = $this->getAwardUser($size);//今天的表记录
    				//更改表
    				$this->updateAwardUserDate($todaydataArray['ids']);
    				//返回结果
    				return $todaydataArray['data'];
    			}
    		}else{
    			$todaydataArray = $this->getAwardUser($size);//今天的表记录
    			//更改表
    			$this->updateAwardUserDate($todaydataArray['ids']);
    			//返回结果
    			return $todaydataArray['data'];
    		}
    	}
    }
    //1.1获取记录20条
    function getAwardUser($size,$yesterdayMaxID=NULL){
    	if ($yesterdayMaxID) {
    		$this->db->where("id >",$yesterdayMaxID);
    	}
    	$this->db->order_by("id","ASC");
    	$query = $this->db->get("zz_extend_useraward",$size);
    	$todaydata2 = $query->result_array();//数据
    	$todaydata2Num = $query->num_rows();//返回今天数据量
    	$todaydata2ids = array();//id数组
    	if ($todaydata2Num==$size) {
    		foreach ($todaydata2 as $item){
    			$todaydata2ids[] = $item['id'];
    		}
    	}
    	return array('data'=>$todaydata2,'num'=>$todaydata2Num,'ids'=>$todaydata2ids);
    }
    //1.2更改表
    function updateAwardUserDate($ids){
    	$today = date("Y-m-d");
    	//更改表
    	$this->db->set("turndate",$today);
    	$this->db->where_in("id",$ids);
    	$this->db->update("zz_extend_useraward");
    	return $this->db->affected_rows();
    }
}